package com.gxuwz.medical.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.gxuwz.medical.database.DBUtil;
import com.gxuwz.medical.domain.area.Area;
import  com.gxuwz.medical.domain.chronicdis.Chronicdis;
import com.gxuwz.medical.domain.vo.Page;
import com.gxuwz.medical.exception.DbException;
/**
 *  慢性病管理类
 * @author 沉羽墨玖
 *
 */
public class ChronicdisDao extends GenericDao<Chronicdis> {
	private static final String SQL_SELECT_ILLCODE="select * from chronicdis where illCode=?";

	@Override
	protected Chronicdis handle(ResultSet rs) throws SQLException {
		try{
			Chronicdis chronicdis=new Chronicdis();
			chronicdis.setIllCode(rs.getString("illCode"));
			chronicdis.setIllName(rs.getString("illName"));
			chronicdis.setPyCode(rs.getString("pyCode"));
			chronicdis.setWbCode(rs.getString("wbCode"));
			return chronicdis;
	}catch(SQLException e){
		throw new SQLException("结果集转换为对象失败:"+e.getMessage(),e);
		
	}
	}
	/**
	 *  查找对应的慢性病记录并转为对象实例
	 * @param illCode
	 * @return
	 * @throws SQLException
	 */
	public Chronicdis get(String illCode)throws SQLException{
		Connection conn=null;
		PreparedStatement pstmt=null;
		ResultSet rs=null;
		try{
			conn=DBUtil.getConn();
			pstmt=conn.prepareStatement(SQL_SELECT_ILLCODE);
			int index=1;
			pstmt.setString(index, illCode);
			rs=pstmt.executeQuery();
			return handle(rs);
		}catch(SQLException e){
			throw new SQLException("读取illCode记录失败",e);
		}
	}
	public List<Chronicdis>findChildChronicdis(String illCode) throws Exception {
		try{
	    String sql="select * from chronicdis where illCode like'"+illCode+"%' ";
		  Object[]params={};
		  return super.queryOjects(sql, params);
		}catch(SQLException e){
			e.printStackTrace();
			throw new DbException("查找子一级慢性病记录失败",e);
		}
	}
	
	public List<Chronicdis> queryObjects(String sql,Object[]params)throws SQLException{
		Connection conn=null;
		PreparedStatement pstmt=null;
		ResultSet rs=null;
		List<Chronicdis> results=new ArrayList<Chronicdis>();
		try{
			conn=DBUtil.getConn();
			pstmt=conn.prepareStatement(sql);
			int index=1;
			if(params!=null){
				for(Object param:params){
					if(param instanceof String){
						pstmt.setString(index++, (String)param);
					}
					if(param instanceof Integer){
						pstmt.setInt(index++, (Integer)param);
					}
					if(param instanceof Float){
						pstmt.setFloat(index++, (Float)param);
					}
				}
				
			}
			rs=pstmt.executeQuery();
			while(rs.next()){
				Chronicdis chronicdis=handle(rs);
				results.add(chronicdis);
			}
			return results;
		}catch(SQLException e){
			throw new SQLException("执行SQL["+sql+"]失败",e);
		}
	}
	/**
	 * 分页
	 * @param 如 select * from chronicdis where illCode=?  
	 * @param params
	 * @param page
	 * @param size
	 * @return
	 * @throws SQLException
	 */
		public Page queryOjectsByPage(String sql,Object[]params,int pageNo,int size)throws SQLException{
			Connection conn =null;
			PreparedStatement pstmt=null;
			ResultSet rs=null;
			Page page=null;
			List<Chronicdis> datas=new ArrayList<Chronicdis>();
			try{
				int start=(pageNo-1)*size;
				conn=DBUtil.getConn();
				pstmt=conn.prepareStatement(sql+" limit "+start+","+size);
				int index=1;
				if(params!=null){
				  for(Object param:params){
					  if(param instanceof String){
						  pstmt.setString(index++, (String)param);
					  }
					  if(param instanceof Integer){
						  pstmt.setInt(index++, (Integer)param);
					  }
					  if(param instanceof Float){
						  pstmt.setFloat(index++, (Float)param);
					  }
				  }
				}
				rs=pstmt.executeQuery();
				while(rs.next()){
					Chronicdis chronicdis=handle(rs);
					datas.add(chronicdis);
				}
				//统计SQL对应记录数
				int total=count(sql, params);
				page=new Page(total, pageNo, size, datas);
				return page;
			}catch(SQLException e){
				throw new SQLException("执行SQL["+sql+"]失败",e);
			}
			
		}

}
